/**
 * 
 */
package com.huawei.esdk.uc.sametime.services;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Display;

import com.huawei.esdk.uc.jna.UCService;
import com.huawei.esdk.uc.jna.Constants.ContactAvailability;
import com.huawei.esdk.uc.jna.mode.CallMember;
import com.huawei.esdk.uc.jna.mode.VideoFrameParmas;
import com.huawei.esdk.uc.sametime.ConstantsDefines.CallMode;
import com.huawei.esdk.uc.sametime.ConstantsDefines.CallStatus;
import com.huawei.esdk.uc.sametime.models.Participant;
import com.huawei.esdk.uc.sametime.models.ParticipantList;
import com.huawei.esdk.uc.sametime.util.ImageFactory;
import com.ibm.collaboration.realtime.people.PeopleUtil;

/**
 * @author y00175154
 * 
 */
public class UCCallService
{
    private static Logger logger = Logger
            .getLogger(UCCallService.class);

    private static int callMode = CallMode.invalid;

    private static int callStatus = CallStatus.end;

    public static void setCallMode(int callmode)
    {
        callMode = callmode;
    }

    public static int getCallMode()
    {
        return callMode;
    }

    public static void setCallStatus(int callstatus)
    {
        callStatus = callstatus;
    }

    public static int getCallStatus()
    {
        return callStatus;
    }
    
    public static void clear()
    {
        setCallMode(CallMode.invalid);
        setCallStatus(CallStatus.end);
    }

    /**
     * 视频呼叫
     * @param participant
     * @param videoParmas
     * @return
     */
    public static boolean startVideoCall(Participant person,
            VideoFrameParmas videoParmas)
    {
        if (null == person || null == videoParmas)
        {
            return false;
        }
        UCCallService.setCallMode(CallMode.callOut);
        UCCallService.setCallStatus(CallStatus.calling);

        CallMember callMember = new CallMember(person.getCallNumType(), person
                .getNumber());
        callMember.setVideoFrameParmas(videoParmas);
        int res = UCService.getInstance().UC_SDK_MakeVideoCall(callMember);

        // 发布自己的状态
        UCService.getInstance().UC_SDK_PubSelfStatus(ContactAvailability.Busy,
                "1");
        PeopleUtil.getLocalPerson().refreshStatus();

        return (res == 0) ? true : false;
    }

    /**
     * 发起语音
     * @param person
     * @return
     */
    public static boolean startCall(ParticipantList persons)
    {
        if (null == persons)
        {
            return false;
        }
        UCCallService.setCallMode(CallMode.callOut);
        UCCallService.setCallStatus(CallStatus.calling);

        // 这里调用UC SDK的VoIP呼叫请求服务接口,返回true：呼叫成功,false:呼叫失败
        // makeCall()...
        CallMember callMember = null;
        // 调用VoIP语音呼叫请求
        List<CallMember> members = new ArrayList<CallMember>();
        for (Participant participant : persons.getparticipants())
        {
            logger.info("caller = " + participant.getNumber());
            callMember = new CallMember(participant.getCallNumType(),
                    participant.getNumber());
            members.add(callMember);
        }
        int ret = UCService.getInstance().UC_SDK_MakeCall(members);

        // 发布自己的状态
        UCService.getInstance().UC_SDK_PubSelfStatus(ContactAvailability.Busy,
                "1");
        PeopleUtil.getLocalPerson().refreshStatus();

        return (ret == 0) ? true : false;
    }

    /**
     * 结束呼叫，如果当前为会议且自己是会议主持人，则结束会议，否则只是自己退出会议。
     * @return
     */
    public static boolean endCall()
    {
        int ret = UCService.getInstance().UC_SDK_HangupCall();
        return (ret == 0) ? true : false;
    }

    /**
     * 呼叫保持/呼叫保持恢复
     * @param isHold
     * @return
     */
    public static boolean holdCall(boolean isHold)
    {
        int ret = -1;
        if (isHold)
        {
            ret = UCService.getInstance().UC_SDK_HoldCall();
        }
        else
        {
            ret = UCService.getInstance().UC_SDK_ResumeCall();
        }
        return (ret == 0) ? true : false;
    }

    /**
     * 静音/取消静音
     * @param isMute
     * @return
     */
    public static boolean mute(boolean isMute)
    {
        int ret = -1;
        if (isMute)
        {
            ret = UCService.getInstance().UC_SDK_MuteMic();
        }
        else
        {
            ret = UCService.getInstance().UC_SDK_UnMuteMic();
        }
        return (ret == 0) ? true : false;
    }

    /**
     * 升级视频通话
     * @param parmas
     * @return
     */
    public static boolean openVideoCall(VideoFrameParmas parmas)
    {
        int res = UCService.getInstance().UC_SDK_MakeVideoCall(parmas);
        return (res == 0) ? true : false;
    }

    /**
     * 移除视频通话
     * @return
     */
    public static boolean colseVideoCall()
    {
        int res = UCService.getInstance().UC_SDK_HangupVideoCall();
        return (res == 0) ? true : false;
    }

    /**
     * 获取单人呼叫状态图标
     * @param status
     * @return
     */
    public static Image getCallStatusImage(int status)
    {
        Image statusImage;
        if (CallStatus.calling == UCCallService.getCallStatus())
        {
            statusImage = getImage(ImageFactory.ICON_CALLING);
        }
        else if (CallStatus.connected == UCCallService.getCallStatus())
        {
            statusImage = getImage(ImageFactory.ICON_UCSTATUS_BUSY);
        }
        else
        {
            statusImage = getImage(ImageFactory.CALL_END);
        }

        return statusImage;
    }

    private static Image getImage(String imgPath)
    {
        return ImageFactory.loadImage(Display.getCurrent(), imgPath);
    }

}
